<template>
  <!-- 封装的设备类型管理的新增组件 -->
  <div>
    <el-dialog
      title="新增人员"
      :visible="visible"
      width="30%"
      @close="$emit('addVisibleFn')"
    >
      <el-form ref="form" :model="iptValue" :rules="form" label-width="80px">
        <el-form-item label="人员名称:" prop="userName" label-width="100px">
          <el-input v-model="iptValue.userName" />
        </el-form-item>
        <el-form-item label="角色:" prop="roleId" label-width="100px">
          <el-select
            v-model="iptValue.roleId"
            placeholder="请选择"
            style="width: 430px"
          >
            <el-option
              v-for="item in roleList"
              :key="item.roleId"
              :label="item.roleName"
              :value="item.roleId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="联系电话" prop="mobile" label-width="100px">
          <el-input v-model="iptValue.mobile" />
        </el-form-item>

        <el-form-item label="负责区域:" prop="regionId" label-width="100px">
          <el-select
            v-model="iptValue.regionId"
            placeholder="请选择"
            style="width: 430px"
          >
            <el-option
              v-for="item in areaList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="头像:" prop="image" label-width="100px">
          <div
            action="#"
            class="avatar-uploader"
            @click="$refs.iptFile.click()"
          >
            <img
              v-if="iptValue.image"
              class="pic"
              :src="iptValue.image"
              alt=""
            >
            <i v-else class="el-icon-plus avatar-uploader-icon" />
            <input
              v-show="false"
              ref="iptFile"
              type="file"
              accept="image/*"
              @change="onFileChange"
            >
          </div>
        </el-form-item>
        <el-form-item label="状态" label-width="100px">
          <!-- `checked` 为 true 或 false -->
          <el-checkbox v-model="iptValue.status">是否启用</el-checkbox>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="$emit('addVisibleFn')">取 消</el-button>
        <el-button type="primary" @click="addSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { ModifierApi, Addrole, areaList, roleList, picApi } from '@/api'
// import { AddTactics } from '@/api'
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {

      row: '',
      areaList: [],
      roleList: [], // 商品类型列表
      iptValue: {
        userName: '', // 用户名
        roleId: '', // 角色ID
        status: false, // 启用状态
        regionName: '', // 所属区域名称
        regionId: '', // 所属区域ID
        mobile: '', // 手机号
        image: '' // 头像
      },
      form: {
        userName: {
          required: true,
          message: '请输入用户名',
          trigger: 'blur'
        },
        roleId: {
          required: true,
          message: '请选择角色',
          trigger: 'blur'
        },
        regionId: {
          required: true,
          message: '请选择区域',
          trigger: 'blur'
        },
        mobile: {
          required: true,
          message: '请输入手机号',
          trigger: 'blur'
        },
        image: {
          required: true,
          message: '请选择头像',
          trigger: 'blur'
        }
      }
    }
  },
  watch: {
    tow() {
      console.log(this.row)
    }
  },
  async created() {
    const res = await roleList()
    console.log(res)
    this.roleList = res.data
    const { data } = await areaList({ pageIndex: 1, pageSize: 1000 })
    this.areaList = data.currentPageRecords
  },
  methods: {
    modifyFn(row) {
      this.row = row
      this.iptValue = {
        userName: row.userName, // 用户名
        roleId: row.roleId, // 角色ID
        status: row.status, // 启用状态
        regionName: row.regionName, // 所属区域名称
        regionId: row.regionId, // 所属区域ID
        mobile: row.mobile, // 手机号
        image: row.image // 头像
      }
    },
    async addSubmit() {
    // 确认
      this.$refs.form.validate(async(valid) => {
        if (!valid) return
        if (this.row.userName) {
          console.log(this.row)
          await ModifierApi(this.row.id, this.iptValue)
          this.$message.success('修改成功')
          this.iptValue = {
            userName: '', // 用户名
            roleId: '', // 角色ID
            status: false, // 启用状态
            regionName: '', // 所属区域名称
            regionId: '', // 所属区域ID
            mobile: '', // 手机号
            image: '' // 头像
          }
          this.$emit('addVisibleFn')
          this.$emit('publicFn')
        } else {
          console.log(this.areaList)
          this.areaList.forEach(item => {
            if (item.id === this.iptValue.regionId) {
              this.iptValue.regionName = item.name
            }
          })
          await Addrole(this.iptValue)
          this.$message.success('添加成功')
          // console.log(this.iptValue)
          this.$emit('addVisibleFn')
          this.$emit('publicFn')
        }
      })
    },
    // 文件选择方法
    async onFileChange(ev) {
      //   console.log(ev.target.files[0])
      if (ev.target.files.length === 0) return // 防止用户未选择图片
      const fd = new FormData()
      fd.append('fileName', ev.target.files[0]) // photo在表单里参数名携带
      const res = await picApi(fd)
      console.log(res)
      // console.log(res.data.data.photo)
      this.iptValue.image = res.data
    }

  }

}

</script>

<style lang="scss" scoped>
.cli {
  background-color: #f3f6fb;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.pic {
  width: 76px;
  height: 76px;
}
</style>
